<?php
class ProfessorsController extends AppController{

	var $uses = array('Professor');

	function beforeFilter(){
		parent::beforeFilter();
	}

	/*
	 * standard method
	 */
	public function index(){
			if(!empty($this->data)){
			
		}
		
		$this->data = $this->Professor->find('all');
	}

	function add(){
		if(!empty($this->data)){
			$ok = $this->Professor->save($this->data);
			$trueMsg = 'Der Dozent wurde angelegt';
			$falseMsg = 'Eingabe unvollst�ndig. Der Dozent konnte nicht angelegt werden. '. $this->Session->error();
			parent::doFlash($ok, $trueMsg, $falseMsg);
			if($ok){
				$this->redirect('/manager/professors/index');
			}
		}
		$ref = $this->referer();
		
	}

	function update($id=null){
		if(is_numeric($id)){
			if(!empty($this->data)){
				$this->Professor->id = $id;
				$ok = $this->Professor->save($this->data);
				$result = $this->Professor->findById($id);
				$name = $result['Professor']['full_name'];
				$trueMessage = 'Der Dozent "'.$name.'" wurde ge&auml;ndert';
				$falseMessage = 'Der Dozent "'.$name.'" konnte nicht ge&auml;ndert werden. '.$this->Session->error();
				parent::doFlash($ok, $trueMessage, $falseMessage);
				if($ok){
					$this->redirect(array('controller'=>'professors','action'=>'index'));
				}
			}
			$this->data = $this->Professor->findById($id);
		}
	}
	
	function delete($id=null){
		
		if(is_numeric($id)){
			$result = $this->Professor->findById($id);
			$ok = empty($result['course']);
			
			if($ok){
				$ok = $this->Professor->delete($id);	
			} 
			
			$falsemessage = 'Der Dozent "'.$result['Professor']['full_name'].'"'; 
			$falsemessage .= ' hat Abh&auml;ngigkeiten und kann nicht gel&ouml;scht werden.';
			$truemessage = 'Der Dozent "'.$result['Professor']['full_name'].'"'; 
			$truemessage .= ' wurde gel&ouml;scht.';
			parent::doFlash($ok, $truemessage, $falsemessage);
			
			$this->redirect(array('controller'=>'Professors','action'=>'index'));
		}
		
	}
}
	?>
